<template>
  <view>
    <view class="user_info">
      <view class="user_icon">
        <image :src="imgDetail.user.avatar" mode="widthFix"></image>
      </view>
      <view class="user_desc">
        <view class="user_name">{{imgDetail.user.name}}</view>
        <view class="user_time">{{imgDetail.cnTime}}</view>
      </view>
    </view>
    <!-- 大图 -->
    <view class="high_img">
      <!-- 子传父 -->
      <slide-image @slide-image='handleSlideImage'>
        <image mode="widthFix" :src="imgDetail.thumb"></image>
      </slide-image>
    </view>
    <!-- 点赞 -->
    <view class="user_rank">
      <view class="rank">
        <text class="iconfont icondianzan">{{imgDetail.rank}}</text>
      </view>
      <view class="user_collect">
        <text class="iconfont iconshoucang">收藏</text>
      </view>
    </view>
    <!-- 专辑 -->
    <view class="album_wrap" v-if="album.length">
      <view class="album_title">相关</view>
      <view class="album_list">
        <view class="album_item" v-for="item in album" :key="item.id">
          <view class="album_cover">
            <image :src="item.cover" mode="aspectFill"></image>
          </view>
          <view class="album_info">
            <view class="album_info_text">专辑</view>
            <view class="album_name">{{item.name}}</view>
            <view class="iconfont iconiconfontjiantou4"></view>
          </view>
        </view>
      </view>
    </view>
    <comment-hot :hot='hot'></comment-hot>
    <comment-hot :comment='comment'></comment-hot>
    <!-- 下载按钮 -->
    <view class="download">
      <view class="download-btn" @click="handleDownLoad">下载图片</view>
    </view>
  </view>
</template>

<script>
import CommentHot from './commenthot'
import slideImage from './slideimage'
import moment from 'moment'
// 设置语言为中文
moment.locale('zh-cn')
export default {
  components: {
    CommentHot,
    slideImage
  },
  data() {
    return {
      // 图片信息对象
      imgDetail: {},
      album: [],
      hot: [],
      comment: [],
      // 图片索引
      imgIndex: 0
    }
  },
  onLoad() {
    const { imgIndex } = getApp().globalData
    this.imgIndex = imgIndex
    this.getData()
  },
  methods: {
    getData() {
      const { imgList } = getApp().globalData
      this.imgDetail = imgList[this.imgIndex]
      this.imgDetail.cnTime = moment(this.imgDetail.atime * 1000).fromNow()
      this.loadComment(this.imgDetail.id)
    },
    async loadComment(id) {
      const { res } = await this.request({ url: `/image/v2/wallpaper/wallpaper/${id}/comment` })
      res.hot.map(v => {
        v.cnTime = moment(v.atime * 1000).fromNow()
      })
      res.comment.map(v => {
        v.cnTime = moment(v.atime * 1000).fromNow()
      })
      this.hot = res.hot
      this.comment = res.comment
      console.log(res)
    },
    handleSlideImage(direction) {
      const { imgList } = getApp().globalData
      if (direction === 'right' && this.imgIndex > 0) {
        this.imgIndex--
        this.getData()
      } else if (direction === 'left' && this.imgIndex < imgList.length - 1) {
        this.imgIndex++
        this.getData()
      } else {
        uni.showToast({ title: '没有数据了', icon: 'none' })
      }
    },
    async handleDownLoad() {
      await uni.showLoading({ title: '下载中' })
      const res = await uni.downloadFile({ url: this.imgDetail.img })
      const { tempFilePath } = res[1]
      const res2 = await uni.saveImageToPhotosAlbum({ filePath: tempFilePath })
      await uni.hideLoading()
      await uni.showToast({ title: '下载成功' })
    }
  }
}
</script>

<style lang="scss" scoped>
.user_info {
  display: flex;
  padding: 20rpx;
  .user_icon {
    padding: 0 20rpx;
    image {
      width: 88rpx;
      border-radius: 50%;
    }
  }
  .user_desc {
    .user_name {
      color: #000;
      font-weight: 600;
    }

    .user_time {
      color: #ccc;
      font-size: 24rpx;
      padding: 10px 0rpx;
    }
  }
}
.user_rank {
  display: flex;
  height: 80rpx;
  border-bottom: 5rpx solid #eee;
  .rank {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
  }

  .user_collect {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
  }
}
.high_img {
  border-bottom: 1rpx solid #eee;
}
.album_wrap {
  padding: 20rpx;
  .album_title {
    padding: 10rpx 0;
  }

  .album_list {
    .album_item {
      display: flex;
      padding: 10rpx 0;
      border-bottom: 10rpx solid #eee;
      .album_cover {
        flex: 1;
        image {
          width: 180rpx;
          height: 180rpx;
        }
      }

      .album_info {
        flex: 3;
        padding-left: 20rpx;
        position: relative;
        .album_info_text {
          width: 100rpx;
          height: 50rpx;
          background-color: $color;
          color: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .album_name {
          padding: 10rpx 0;
          color: #888;
        }
        .iconfont {
          font-size: 40rpx;
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: 10%;
          color: #888;
        }
      }
    }
  }
}
.download {
  height: 120rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .download-btn {
    width: 90%;
    height: 75%;
    background-color: $color;
    color: #fff;
    font-size: 50rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>